<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Exception;
use app\admin\model\Cuser as Cusers;

class Muser extends Common
{
    private $cModel;   //当前控制器关联模型

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Cusers;   //别名：避免与控制名冲突
    }

    public function index()
    {
        $where = [];
        if(input('get.search')){
            $where['telphone'] = ['like', '%'.input('get.search').'%'];

        }
        $order = ['id desc'];

        $dataList = $this->cModel->where($where)->where(['type'=>1])->order($order)->paginate('',false, page_param());
        $this->assign('dataList',$dataList);
        return $this->fetch();

    }

    public function create()
    {
        if(\request()->isPost()){
            Db::startTrans();
            try {
             $data = input('post.');
             $result = $this->cModel->validate(CONTROLLER_NAME.'.add')->allowField(true)->save($data);
             if ($result) {
                Db::commit();
                 return ajaxReturn(lang('action_success'),url('index'));
             } else {
                 return ajaxReturn($this->cModel->getError());
             }

            } catch (Exception $e) {
                  Db::rollback();
                  return ajaxReturn($e->getMessage());
            }
        }else{
            return $this->fetch();
        }
    }

    public function edit($id)
    {
        if(request()->isPost()){
                $data = input('post.');
            if ($data['actions'] == 'password') {
                $oldData = $this->cModel->where(['id'=>$data['id'],'password'=>md5($data['oldpassword'])])->find();
                if(empty($oldData)){
                    return ajaxReturn(lang('oldpassword_val'));
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.password')->allowField(true)->save($data,$data['id']);
            }elseif($data['actions']=='muser'){
                unset($data['actions']);
                $where = ['id'=>$data['id']];
                $result = $this->cModel->allowField(true)->where($where)->update($data);
            }


            if ($result) {
                return ajaxReturn(lang('action_success'),url('index'));
            } else {
                return ajaxReturn($this->cModel->getError());
            }



        }else{
            if($id >0){
                $data = $this->cModel->get($id);
                $this->assign('data', $data);
                return $this->fetch();
            }

        }
    }

    public function delete()
    {
        if(request()->isPost()){
            $id = input('id');
            if(isset($id) && !empty($id)){
                $id_arr = explode(',',$id);
                $where = ['id'=>['in',$id_arr]];
                $result = $this->cModel->where($where)->delete();
                if ($result) {
                    return ajaxReturn(lang('action_success'),url('index'));
                } else {
                    return ajaxReturn($this->cModel->getError());
                }
            }
        }

    }
}